/// <reference path="tfcore.js"/>
///
/// Method to toggle a node in the tree by displaying or hiding relevant elements.
///
function edToggle(item)
{
    
	var div=document.getElementById("D" + item);
	var visible=(div.style.display!="none");
	var key=document.getElementById("P" + item);
	var closed=document.getElementById("C" + item);
	var open=document.getElementById("O" + item);
	
	// Check if the item clicked has any children. If it does not then remove the plus/minus icon
	// and replace it with a transaparent gif.
	var removeIcon = div.hasChildNodes() == false;
	
	if( key != null )
	{
		if( !removeIcon )
		{
			if (visible)
			{
		 		// Hide open icon and the node contents
		 		div.style.display="none";
		 		open.style.display="none";
		 		// Show the closed icon
		 		closed.style.display="block";
		 		key.innerHTML="<img src='img/plus.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
			}
			else
			{
			    // Show node contents and the icon
		  		div.style.display="block";
		 		open.style.display="block";
		 		// Hide the closed icon
		 		closed.style.display="none";
				key.innerHTML="<img src='img/minus.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
			}
		}
		else
			key.innerHTML="<img src='img/transparent.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
	}
}


///
/// Perform an AJAX style request for the contents of a node and put the contents
/// into the empty div.
///
function DelayLoadNode(treeid, item)
{
    var div=document.getElementById("D" + item);
    var xmlhttp = getXMLHTTP();

    // Make sure the node is empty really, and if so fill it
    if (div.innerHTML == "")
    {
        xmlhttp.open("GET", "TreeFill.aspx?tree="+treeid+"&id="+item, true);
        xmlhttp.onreadystatechange=function() 
        {
            if (xmlhttp.readyState==4)
            {
                // DEBUG: alert(xmlhttp.responseText);
                div.innerHTML = xmlhttp.responseText;
                edToggle(item);
            }
        }
        xmlhttp.send(null)
    }
    else // The node is already populated
    {
        edToggle(item);
    }
}